package com.biscuit.page.basicservice.utils;

import com.biscuit.common.base.BaseAMO;
import com.biscuit.common.base.BaseResponse;
import com.biscuit.page.basicservice.model.enums.BaseResponseEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * @author mazihao
 * @createTime 2024/3/7 17:45
 */
public class BaseResponseUtil {

    private BaseResponseUtil() {}

    public static <T extends BaseAMO> BaseResponse<T> success(T data) {
        BaseResponse<T> response = build(BaseResponseEnum.SUCCESS);
        response.setData(data);
        return response;
    }

    public static <T extends BaseAMO> BaseResponse<T> success() {
        return success(null);
    }

    public static <T extends BaseAMO> BaseResponse<T> fail(String message) {
        BaseResponse<T> response = fail();
        if (StringUtils.isNotBlank(message)) {
            response.setMessage(message);
        }
        return response;
    }

    public static <T extends BaseAMO> BaseResponse<T> fail(BaseResponseEnum baseResponseEnum) {
        return build(baseResponseEnum);
    }

    public static <T extends BaseAMO> BaseResponse<T> fail(BaseResponseEnum baseResponseEnum, String message) {
        BaseResponse<T> response = build(baseResponseEnum);
        response.setMessage(StringUtils.isNotBlank(message) ? message : baseResponseEnum.getMessage());
        return response;
    }

    public static <T extends BaseAMO> BaseResponse<T> fail() {
        return build(BaseResponseEnum.ERROR);
    }

    private static <T extends BaseAMO> BaseResponse<T> build(String code, String message) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    private static <T extends BaseAMO> BaseResponse<T> build(BaseResponseEnum baseResponseEnum) {
        return build(baseResponseEnum.getCode(), baseResponseEnum.getMessage());
    }

}
